package com.softeem.score.manager.controller.admin;


import com.github.pagehelper.PageInfo;
import com.softeem.score.common.log.annotation.Log;
import com.softeem.score.common.log.enums.OperatorType;
import com.softeem.score.manager.service.SysUserService;
import com.softeem.score.model.dto.AssginRoleDto;
import com.softeem.score.model.dto.SysUserDto;
import com.softeem.score.model.entity.system.SysUser;
import com.softeem.score.model.vo.common.Result;
import com.softeem.score.model.vo.common.ResultCodeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhu
 * @since 2024-02-14
 */
@Tag(name = "用户管理")
@RestController
@RequestMapping(value = "/admin/system/sysUser")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    @Log(title = "分页查询角色列表", businessType = 0, operatorType = OperatorType.MANAGE)
    @Operation(summary = "分页查询角色列表")
    @GetMapping(value = "/findByPage/{pageNum}/{pageSize}")
    public Result<PageInfo<SysUser>> findByPage(@PathVariable(value = "pageNum") Integer pageNum,
                                                @PathVariable(value = "pageSize") Integer pageSize,
                                                SysUserDto sysUserDto) {
        PageInfo<SysUser> pageInfo = sysUserService.findByPage(pageNum, pageSize, sysUserDto);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    @Log(title = "保存用户", businessType = 1, operatorType = OperatorType.MANAGE)
    @Operation(summary = "保存用户")
    @PostMapping(value = "/saveSysUser")
    public Result saveSysUser(@RequestBody SysUser sysUser) {
        sysUserService.saveSysUser(sysUser);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Log(title = "更新用户", businessType = 2, operatorType = OperatorType.MANAGE)
    @Operation(summary = "更新用户")
    @PutMapping(value = "/updateSysUser")
    public Result updateSysUser(@RequestBody SysUser sysUser) {
        sysUserService.updateSysUser(sysUser);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Log(title = "更新用户状态", businessType = 2, operatorType = OperatorType.MANAGE)
    @Operation(summary = "更新用户状态")
    @PutMapping(value = "/updateStatus/{userId}/{status}")
    public Result updateStatus(@PathVariable Long userId, @PathVariable Integer status) {
        sysUserService.updateStatus(userId, status);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Log(title = "更新用户头像", businessType = 2, operatorType = OperatorType.OTHER)
    @Operation(summary = "更新用户头像")
    @PutMapping(value = "/updateAvatar/{userId}")
    public Result updateAvatar(@PathVariable Long userId,
                               @RequestBody Map<String, String> requestData) {
        sysUserService.updateAvatar(userId, requestData.get("avatar"), requestData.get("token"));
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Log(title = "更新用户手机号", businessType = 2, operatorType = OperatorType.OTHER)
    @Operation(summary = "更新用户手机号")
    @PutMapping(value = "/updateMobile/{username}")
    public Result updateMobile(@PathVariable String username,
                               @RequestBody Map<String, String> requestData) {
        sysUserService.updateMobile(username, requestData.get("mobile"), requestData.get("code"), requestData.get("token"));
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Log(title = "通过id删除用户", businessType = 3, operatorType = OperatorType.MANAGE)
    @Operation(summary = "通过id删除用户")
    @DeleteMapping(value = "/deleteById/{userId}")
    public Result deleteById(@PathVariable(value = "userId") Long userId) {
        sysUserService.deleteById(userId);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Log(title = "分配用户角色", businessType = 2, operatorType = OperatorType.MANAGE)
    @Operation(summary = "分配用户角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleDto assginRoleDto) {
        sysUserService.doAssign(assginRoleDto);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }
}

